package com.vesper.mysdk;

import android.util.Log;

import java.util.ArrayList;
import java.util.List;
public class RTCSDKImpl extends RTCSDK {
    public static final String TAG = "MSDKImpl";
    private List<MCallback> callbacks = new ArrayList<MCallback>();
    public int status = STATUS_NOT_INIT;

    public RTCSDKImpl(String secretKey, String appKey, MCallback callback) {
        if (!callbacks.contains(callback)) {
            callbacks.add(callback);
        }
        //模拟校验秘钥
        if (checkKeys(secretKey, appKey)) {
            notifyInit(true, RTCSDK.REASON_NORMAL);
            int old = status;
            status = STATUS_INITED;//修改当前状态
            notifyStatusChanges(old, status);
        } else {
            notifyInit(false, RTCSDK.MREASON_ACCOUNT_OR_PASSWORD_WRONG);
            callbacks.remove(callback);
        }
    }

    @Override
    public int getLoginStatus() {
        return status;
    }

    @Override
    public void login(String uid, String password) {
        if (status <= STATUS_NOT_INIT) {
            notifyLogin(false, REASON_NOT_INIT);
        } else if (status == STATUS_INITED || status == STATUS_NOT_LOGIN) {
            int old = status;
            status = STATUS_LOGINED;//修改当前状态
            notifyStatusChanges(old, status);
            notifyLogin(true, REASON_NORMAL);
        } else if (status > STATUS_NOT_LOGIN) {
            notifyLogin(false, REASON_ALREADY_LOGIN);
            Log.e(TAG, "login: sdk already logined!");
        }
    }

    @Override
    public void logout() {
        if (status == STATUS_LOGINED) {
            int old = status;
            status = STATUS_NOT_LOGIN;
            notifyLogout(true, REASON_NORMAL);
            notifyStatusChanges(old, status);
        } else {
            notifyLogout(false, REASON_NOT_LOGIN);
        }
    }

    @Override
    public void destroySDK() {
        Log.i(TAG, "destroy");
        if (status > 0) {
            //上报状态改变
            int old = status;
            status = STATUS_NOT_INIT;
            notifyStatusChanges(old, status);
            notifyLogout(true, REASON_NORMAL);//上报sdk退出登录
            callbacks.clear();//清除回调
        }
    }


    private void notifyInit(boolean b, @MReason final int reason) {
        Log.i(TAG, "notifyInit: res=$res,reason=$reason");
        if (callbacks.size() > 0) {
            for (MCallback callback : callbacks) {
                callback.onLogin(b, reason);
            }
        }
    }

    private void notifyLogin(boolean res, @MReason final int reason) {
        Log.i(TAG, "notifyLogin: res=$res,reason=$reason");
        if (callbacks.size() > 0) {
            for (MCallback callback : callbacks) {
                callback.onLogin(res, reason);
            }
        }
    }

    private void notifyLogout(boolean res, @MReason final int reason) {
        Log.i(TAG, "notifyLogout: res=$res,reason=$reason");
        if (callbacks.size() > 0) {
            for (MCallback callback : callbacks) {
                callback.onLogout(res, reason);
            }
        }
    }

    private void notifyStatusChanges(int old, @SDKStatus final int status) {
        Log.i(TAG, "notifyLogin: res=$res,reason=$reason");
        if (callbacks.size() > 0) {
            for (MCallback callback : callbacks) {
                callback.onLoginStatusChanged(old, status);
            }
        }
    }


    /**
     * 模拟校验秘钥
     *
     * @param secretKey
     * @param appKey
     * @return
     */
    private boolean checkKeys(String secretKey, String appKey) {
        if (secretKey != "123") {
            Log.e(TAG, "checkKeys: secreKey is not right");
            return false;
        }
        if (appKey != "123") {
            Log.e(TAG, "checkKeys: secreKey is not right");
            return false;
        }
        return true;
    }
}
